<template>
  <el-drawer class="drawer-container" :title="title" :visible.sync="visible" append-to-body width="680px">
    <el-form ref="form" :model="formdata" :rules="formrule" label-width="80px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="渠道名称" prop="name">
            <el-input v-model="formdata.name" placeholder="请输入渠道名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="渠道编码" prop="code">
            <el-input v-model="formdata.code" placeholder="请输入渠道编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="渠道类型" prop="type">
            <el-select v-model="formdata.type" placeholder="请选择公告类型">
              <el-option v-for="item in localDict.notice_type" :key="item.value" :label="item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="formdata.status">
              <el-radio-button v-for="item in localDict.notice_status" :key="item.value" :label="item.value">
                {{ item.label }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="内容">
        <editor v-model="formdata.content" :min-height="192"/>
      </el-form-item>
      <el-form-item label="内容" prop="remark">
        <el-input type="textarea" v-model="formdata.remark" rows="3"/>
      </el-form-item>
    </el-form>
    <div class="drawer-footer">
      <el-button size="mini" :loading="loading" type="primary" @click="handleSubmit">确定</el-button>
      <el-button size="mini" @click="handleCancel">取消</el-button>
    </div>
  </el-drawer>
</template>

<script>

import {getOne, save} from "@/api/base";

export default {
  name: "NoticeModel",
  data() {
    return {
      title: '',
      visible: false,
      loading: false,
      editable: false,
      baseUrl: '/msg/notice',
      formdata: {},
      formrule: {
        code: [
          {required: true, message: '组织编码不能为空', trigger: 'blur'},
          {pattern: /^\w{2,30}$/, message: '组织编码只能包含数字、字母、下划线,长度在2-30个字符', trigger: 'blur'}
        ],
        name: {required: true, message: '组织名称不能为空', trigger: 'blur'},
        type: {required: true, message: '组织类型不能为空', trigger: 'blur'},
        status: {required: true, message: '组织状态不能为空', trigger: 'blur'},
      },
      localDict: {
        notice_type: [],
        notice_status: []
      },
    }
  },
  created() {
    this.getLocalDict();
  },
  methods: {
    add() {
      this.resetForm('form')
      this.formdata = {}
      this.visible = true
      this.title = '新增渠道'
    },
    edit(id) {
      this.resetForm('form')
      getOne(this.baseUrl, id).then(res => {
        this.formdata = res.data
        this.editable = true
        this.visible = true
        this.title = '修改渠道'
      })
    },
    async getLocalDict() {
      await this.getStaticData('msg-channel-type').then(res => {
        this.localDict.channel_type = res.data
      })
      await this.getStaticData('system-status').then(res => {
        this.localDict.system_status = res.data
      })
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          save(this.baseUrl, this.formdata).then(() => {
            this.$modal.msgSuccess("保存成功");
            this.visible = false;
            this.resetForm('form')
            this.$emit('ok')
          }).finally(() => {
            this.loading = false
          });
        }
      });
    },
    handleCancel() {
      this.visible = false
      this.resetForm('form')
      this.$emit('close')
    }
  }
}
</script>
